/*
 * My12306App.java
 */
package my12306;

import java.util.EventObject;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import my12306.db.SQLite;
import my12306.http.HttpClientUtils;
import org.apache.http.client.HttpClient;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.slf4j.LoggerFactory;

/**
 * The main class of the application.
 */
public class My12306App extends SingleFrameApplication {

    private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
    private static TrainClient trainClient;
    private static HttpClient httpClient;
    private static ImageThread imageThread;
    private static AudioThread audioThread;

    /**
     * @return the trainClient
     */
    public static TrainClient getTrainClient() {
        return trainClient;
    }

    /**
     * @param aTrainClient the trainClient to set
     */
    public static void setTrainClient(TrainClient aTrainClient) {
        trainClient = aTrainClient;
    }

    /**
     * @return the httpClient
     */
    public static HttpClient getHttpClient() {
        return httpClient;
    }

    /**
     * @param aHttpClient the httpClient to set
     */
    public static void setHttpClient(HttpClient aHttpClient) {
        httpClient = aHttpClient;
    }

    /**
     * @return the imageThread
     */
    public static ImageThread getImageThread() {
        if (imageThread == null) {
            imageThread = new ImageThread();
            imageThread.start();
        }
        return imageThread;
    }

    /**
     * @return the audioThread
     */
    public static AudioThread getAudioThread() {
        if (audioThread == null) {
            audioThread = new AudioThread();
            audioThread.start();
        }
        return audioThread;
    }

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        addExitListener(new ExitListener() {
            public boolean canExit(EventObject event) {
                return JOptionPane.showConfirmDialog(null, "Do you really want to exit?") == JOptionPane.YES_OPTION;
            }

            public void willExit(EventObject event) {
            }
        });
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    log.info("系统正在退出。");
                    My12306App.getImageThread().setStop();
                    My12306App.getAudioThread().setStop();
                    trainClient.getHttpclient().getConnectionManager().shutdown();
                    SQLite.close();
                    log.info("系统已成功退出。");
                } catch (Exception ex) {
                    log.error(ex.getLocalizedMessage(), ex);
                }
            }
        });
//        show(new My12306View(this));
//        show(new My12306ViewMini(this));
        LoginFrame login = new LoginFrame();
        getImageThread().setUpdator(login);
        show(login);
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     *
     * @return the instance of My12306App
     */
    public static My12306App getApplication() {
        return Application.getInstance(My12306App.class);
    }

    public void showComp(JComponent comp) {
        super.show(comp);
    }

    /**
     * Main method launching the application.
     */
    public static void main(final String[] args) {
        setHttpClient(HttpClientUtils.getHttpsClient());
        setTrainClient(new TrainClient(getHttpClient()));
        launch(My12306App.class, args);
    }
}
